﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Common;
using System.Data.Entity;
using DAO;
using System.Data;
using System.Data.Objects;

namespace BUS
{
    public class BUS_WRUSER
    {
        WRMOEntities db = new WRMOEntities();
        public IList<WRUSER> _GetList()
        {
            return db.WRUSERs.ToList();
        }

        public DataTable Get_User_List()
        {
            IList<Get_User_List_Result> query = db.Get_User_List().ToList();
            return Common.Common.ConvertToDataTable(query.ToList());
        }

        public IList<WRUSER> _GetList(Func<WRUSER, bool> cond)
        {
            return db.WRUSERs.Where(cond).ToList();
        }

        public WRUSER _GetObject(int ID)
        {
            return db.WRUSERs.Where(p => p.ID == ID).FirstOrDefault();
        }

        public WRUSER _GetObject(Func<WRUSER, bool> cond)
        {
            return db.WRUSERs.Where(cond).FirstOrDefault();
        }

        public WRUSER _CreateObject()
        {
            return db.WRUSERs.CreateObject();
        }

        public void _Save()
        {
            db.SaveChanges();

        }

        public void _Add(WRUSER entity, bool save = true)
        {
            try
            {
                db.AddToWRUSERs(entity);
                if (save)
                    _Save();
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }

        public void _Update(WRUSER entity)
        {
            try
            {
                WRUSER _mainEntity = db.WRUSERs.Where(p => p.ID == entity.ID).FirstOrDefault();
                if (_mainEntity != null)
                {
                    _mainEntity.ID = entity.ID;
                    _mainEntity.Username = entity.Username;
                    _mainEntity.Firstname = entity.Firstname;
                    _mainEntity.Lastname = entity.Lastname;
                    _mainEntity.BirthDay = entity.BirthDay;
                    _mainEntity.Email = entity.Email;
                    _mainEntity.Address = entity.Address;
                    _mainEntity.Phone = entity.Phone;
                    _mainEntity.Avatar = entity.Avatar;
                    _mainEntity.RsPssQuestion = entity.RsPssQuestion;
                    _mainEntity.RsPssAnswer = entity.RsPssAnswer;
                    _mainEntity.IDCitizen = entity.IDCitizen;
                    _mainEntity.IDUserGroup = entity.IDUserGroup;
                    _mainEntity.UpdateDate = entity.UpdateDate;
                    _mainEntity.IDUpdateBy = entity.IDUpdateBy;
                    _mainEntity.Description = entity.Description;
                    _mainEntity.IP = entity.IP;
                    _mainEntity.Active = entity.Active;
                }
                _Save();
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }
        public void _Update(int Id, string pass)
        {
            try
            {
                WRUSER _mainEntity = db.WRUSERs.Where(p => p.ID == Id).FirstOrDefault();
                if (_mainEntity != null)
                {
                    _mainEntity.ID = Id;
                    _mainEntity.Password = pass;
                }
                _Save();
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }

        public void _Delete(int ID)
        {
            try
            {
                WRUSER _mainEntity = db.WRUSERs.Where(p => p.ID == ID).FirstOrDefault();
                if (_mainEntity != null)
                {
                    db.WRUSERs.DeleteObject(_mainEntity);
                }
                _Save();
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }

        public void _Delete(WRUSER entity)
        {
            try
            {
                WRUSER _mainEntity = db.WRUSERs.Where(p => p.ID == entity.ID).FirstOrDefault();
                if (_mainEntity != null)
                {
                    db.WRUSERs.DeleteObject(_mainEntity);
                }
                _Save();
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }
    }
}
